﻿using System;
using System.Web;
using System.Web.Caching;

namespace Aj.Core.Cache
{

    public class WebCache : ICache
    {
        public virtual void AddObject(string key, object item)
        {
            AddObject(key, item, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, null);
        }
        public virtual void AddObject(string key, object item, DateTime expire)
        {
            HttpRuntime.Cache.Insert(key, item, null, expire, System.Web.Caching.Cache.NoSlidingExpiration);
        }
        public virtual void AddObject(string key, object item, int expire)
        {
            AddObject(key, item, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(expire), null);
        }
        public virtual void AddObject(string key, object item, string[] files)
        {
            CacheDependency dep = new CacheDependency(files, DateTime.Now);
            AddObject(key, item, dep, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, OnRemove);
        }
        public virtual void AddObject(string key, object item, string[] files, int expire)
        {
            CacheDependency dep = new CacheDependency(files, DateTime.Now);
            AddObject(key, item, dep, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(expire), OnRemove);
        }
        public virtual void AddObject(string key, object item, string[] files, string[] dependKey)
        {
            CacheDependency dep = new CacheDependency(files, dependKey, DateTime.Now);
            AddObject(key, item, dep, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, OnRemove);
        }
        public virtual void AddObject(string key, object item, string[] files, string[] dependKey, int expire)
        {
            CacheDependency dep = new CacheDependency(files, dependKey, DateTime.Now);
            AddObject(key, item, dep, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(expire), OnRemove);
        }
        private static void AddObject(string key, object item, CacheDependency dep, DateTime absoluteExpiration,
                           TimeSpan slidingExpiration, CacheItemRemovedCallback onRemoveCallback)
        {
            if (key == null || item == null)
            {
                return;
            }
            HttpRuntime.Cache.Insert(key, item, dep, absoluteExpiration, slidingExpiration, CacheItemPriority.High, onRemoveCallback);
        }
        public virtual void AddObjectWithDepend(string key, object item, string[] dependKey)
        {
            AddObject(key, item, null, dependKey);
        }
        public void AddObjectWithDepend(string key, object item, string[] dependKey, int expire)
        {
            AddObject(key, item, null, dependKey, expire);
        }
        public virtual void RemoveObject(string key)
        {
            if (key == null)
            {
                return;
            }
            HttpRuntime.Cache.Remove(key);
        }
        public virtual object GetObject(string key)
        {
            if (key == null)
            {
                return null;
            }
            return HttpRuntime.Cache.Get(key);
        }
        public virtual void FlushAll()
        {
            System.Collections.IDictionaryEnumerator cacheEnum = HttpRuntime.Cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                HttpRuntime.Cache.Remove(cacheEnum.Key.ToString());
            }
        }
        public bool Contains(string key)
        {
            return (GetObject(key) != null);
        }
        protected virtual void OnRemove(string key, object item, CacheItemRemovedReason reason)
        {
        }
    }
}
